//
//  YbrBgkDao.m
//  wifibox
//
//  Created by os X 10.9 on 14-12-4.
//  Copyright (c) 2014年 yinbaner. All rights reserved.
//

#import "YbrBgkDao.h"
#import "YbrTimerUtil.h"
#import "YbrFaviItem.h"

@implementation YbrBgkDao


-(BOOL) isAddYet:(int)_id {
    __block BOOL isOk = false;
    
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        FMResultSet *rs;
        // 1.查询数据
        rs = [db executeQueryWithFormat:@"select * from bgk_music where musicid=%i",_id];
        isOk = rs;
        if(rs.next) {
//            isOk = true;
        }
        [rs close];
    }];
    if (isOk) {
        YbrLog(@"好的");
    }else{
        YbrLog(@"不好");
    }
    return isOk;
}


//// 我喜欢的音乐的列表
//NSString * fav_music_sql                =   @"CREATE TABLE IF NOT EXISTS fav_music (_id  INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,sort INTEGER ,name  TEXT);";
//// 我喜欢的音乐列表和在线音乐的中间表
//NSString * bgk_music_sql                =   @"CREATE TABLE IF NOT EXISTS bgk_music(_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT,url TEXT,timerstr  TEXT,duration INTEGER ,musicid  TEXT,favmusicid INTEGER);";



-(BOOL) isHavePlaylistName:(int) musicid playlistId:(int) playlistId {
    __block BOOL isOk = false;
    
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        FMResultSet *rs;
        // 1.查询数据
        rs = [db executeQueryWithFormat:@"select favmusicid from bgk_music where musicid=%i and favmusicid=%i" ,musicid ,playlistId];
        
        while (rs.next) {
            isOk = YES;
            break;
        }
        [rs close];
    }];
    return isOk;
    
}


-(BOOL) addToBgk:(YbrClassInfoItem *) ybrclassInfoItem {
    __block BOOL isOk = false;
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        FMResultSet *rs;
        // 1.查询数据
        rs = [db executeQueryWithFormat:@"insert into bgk_music (name ,url, timerstr ,duration ,musicid,favmusicid) VALUES (%@,%@,%@,%i,%i,%i)",
              ybrclassInfoItem.name,
              ybrclassInfoItem.audio_url ,
              ybrclassInfoItem.timelength ,
              (int)[YbrTimerUtil makeTimeInt:ybrclassInfoItem.timelength],
              ybrclassInfoItem.ID,
              ybrclassInfoItem.favmusicid];
        isOk = rs;
        while (rs.next) {
//            isOk = YES;
//            break;
        }
        [rs close];
    }];
    if (isOk) {
        YbrLog(@"好的时");
    }else{
        YbrLog(@"不好的");
    }

    return isOk;
}

// Arr的对象是  YbrClassInfoItem
-(NSArray *) getAllBgk {
    __block NSArray *arr;
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        FMResultSet *rs;
        // 1.查询数据
        rs = [db executeQueryWithFormat:@"select * from bgk_music"];
        
        NSMutableArray *tempArr = [NSMutableArray array];
        while (rs.next) {
            YbrClassInfoItem *item = [[YbrClassInfoItem alloc] init];
            item.name = [rs stringForColumn:@"name"];
            item.ID = [rs intForColumn:@"musicid"];
            item.audio_url = [rs stringForColumn:@"url"];
            item.timelength = [rs stringForColumn:@"timerstr"];
            item.duration = [rs intForColumn:@"duration"];
            item.favmusicid = [rs intForColumn:@"favmusicid"];
            [tempArr addObject:item];
        }
        [rs close];
        arr = [tempArr copy];
    }];
    
    return arr;
}

+(NSArray *) getAllBgkByFavmusicid:(int)favId
{
    __block NSArray *arr;
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        FMResultSet *rs;
        // 1.查询数据
        rs = [db executeQueryWithFormat:@"select * from bgk_music where favmusicid = %i",favId];
        
        NSMutableArray *tempArr = [NSMutableArray array];
        while (rs.next) {
            YbrClassInfoItem *item = [[YbrClassInfoItem alloc] init];
            item.name = [rs stringForColumn:@"name"];
            item.ID = [rs intForColumn:@"musicid"];
            item.audio_url = [rs stringForColumn:@"url"];
            item.timelength = [rs stringForColumn:@"timerstr"];
            item.duration = [rs intForColumn:@"duration"];
            item.favmusicid = [rs intForColumn:@"favmusicid"];
            [tempArr addObject:item];
        }
        [rs close];
        arr = [tempArr copy];
    }];
    
    return arr;

}

-(YbrClassInfoItem *) getMusicById:(int) _id {
    __block YbrClassInfoItem *myClassInfoItem;
    
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        FMResultSet *rs;
        // 1.查询数据
        rs = [db executeQueryWithFormat:@"select * from bgk_music where musicid=%i" ,_id];
        
        while (rs.next) {
            YbrClassInfoItem *item = [[YbrClassInfoItem alloc] init];
            item.name = [rs stringForColumn:@"name"];
            item.ID = [rs intForColumn:@"musicid"];
            item.audio_url = [rs stringForColumn:@"url"];
            item.timelength = [rs stringForColumn:@"timerstr"];
            item.duration = [rs intForColumn:@"duration"];
            item.favmusicid = [rs intForColumn:@"favmusicid"];
            myClassInfoItem = item;
            break;
        }
        [rs close];
    }];
    
    return myClassInfoItem;
}

-(BOOL) removeMusicById:(int) musicId {
    __block BOOL isOk = false;
    
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        isOk = [db executeUpdateWithFormat:@"delete from bgk_music where musicid=%i",musicId];
    }];
    return isOk;
}

+(BOOL) removeMusicByFavmusicid:(int) favmusicid {
    __block BOOL isOk = false;
    
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        isOk = [db executeUpdateWithFormat:@"delete from bgk_music where favmusicid = %i",favmusicid];
    }];
    return isOk;
}

+(int)insert:(YbrFaviItem *)item
{
    __block int playId;
    
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        [db executeUpdate:@"insert into fav_music (name) VALUES (?)", item.name];
        
        FMResultSet *rs;
        rs = [db executeQueryWithFormat:@"select _id from fav_music where name=%@",item.name];
        
        while (rs.next) {
            playId = [rs intForColumn:@"_id"];
            break;
        }
        [rs close];
    }];
    
    return playId;
}

+(BOOL)update:(YbrFaviItem *)item
{
    __block BOOL isOk = false;
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        FMResultSet *rs;
        // 1.查询数据
        rs = [db executeQueryWithFormat:@"update fav_music set name=%@ where _id=%i",item.name,item._id];
    
        while (rs.next) {
            isOk = YES;
            break;
        }
        [rs close];
    }];
    return isOk;

}

+(BOOL)deletes:(YbrFaviItem *)item
{
    __block BOOL isOk = false;
    
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        isOk = [db executeUpdateWithFormat:@"delete from fav_music where _id = %i",item._id];
    }];
    return isOk;
}

+(NSArray *) getAllFavMusic
{
    __block NSArray *arr;
    [[YbrSQLHelper getDataBase] inDatabase:^(FMDatabase *db) {
        FMResultSet *rs;
        // 1.查询数据
        rs = [db executeQueryWithFormat:@"select * from fav_music"];
        
        NSMutableArray *tempArr = [NSMutableArray array];
        while (rs.next) {
            YbrFaviItem *item = [[YbrFaviItem alloc] init];
            item.name = [rs stringForColumn:@"name"];
            item._id = [rs intForColumn:@"_id"];
            item.sort = [rs intForColumn:@"sort"];
        
            [tempArr addObject:item];
        }
        [rs close];
        arr = [tempArr copy];
    }];
    
    return arr;

}
@end
